<?php
namespace Controllers;
use Resources, Models, Libraries;

class Trnlm extends Resources\Controller
{    
	public function __construct(){
		parent::__construct();
		$this->session = new Resources\Session;
		$this->rest = new Resources\Rest;
		$this->request = new Resources\Request;
		$this->table = new Models\Trnlm;
		$this->DT = new Libraries\Datatables;
	}
	
	public function index(){
		$data['title'] = 'Data TRNLM';
		$data['halaman']='trnlm';
		$this->output('default/template',$data);
	}	
	
	public function init($action='read'){    
        if (IS_AJAX) {
			$action = $this->request->post('action');
			switch ($action){
				default:
					$this->_read();
				break;
				case 'read':
					$this->_read();
				break;
				case 'create':
					$this->_create($this->request->post('data'));
				break;
				case 'edit':
					$this->_update($this->request->post('id'));
				break;
				case 'remove':
					$this->_delete($this->request->post('data'));
				break;
			}
		}
    }
	
	public function _read(){
		$this->DT->select(array('THSMSTRNLM','KDPTITRNLM','KDJENTRNLM','KDPSTTRNLM','NIMHSTRNLM','KDKMKTRNLM','NLAKHTRNLM','BOBOTTRNLM','KELASTRNLM'))
			->from('trnlm');
		echo $this->DT->generate();
	}
	
	private function _update($id){
		$data = $this->rest->getRequest();
		$data['requestMethod'] = $this->rest->requestMethod;
		$doupdate= $this->request->post('data');
		$dowhere = array('id'=> $this->request->post('id',FILTER_SANITIZE_NUMBER_INT));
		$this->table->update($doupdate,$dowhere);
		$this->outputJSON($data, 200);
	}
	
	private function _delete($dowhere){
		$data = $this->rest->getRequest();
		$data['requestMethod'] = $this->rest->requestMethod;
		$this->table->delete($dowhere);
		$this->outputJSON($data, 200);
	}
	
	private function _create($fields){
		$data = $this->rest->getRequest();
		$data['requestMethod'] = $this->rest->requestMethod;
		$this->table->create($fields);
		$this->outputJSON($data, 200);
	}

} 